package com.nsgf.library.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.google.gson.JsonObject;

import com.zhht.htzx.R;
import com.nsgf.library.magicindicator.MagicIndicator;
import com.nsgf.library.magicindicator.ViewPagerHelper;
import com.nsgf.library.magicindicator.buildins.UIUtil;
import com.nsgf.library.magicindicator.buildins.commonnavigator.CommonNavigator;
import com.nsgf.library.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import com.nsgf.library.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import com.nsgf.library.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import com.nsgf.library.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import com.nsgf.library.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import com.nsgf.library.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;
import com.nsgf.library.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;
import com.nsgf.library.magicindicator.buildins.commonnavigator.titles.badge.BadgeAnchor;
import com.nsgf.library.magicindicator.buildins.commonnavigator.titles.badge.BadgePagerTitleView;
import com.nsgf.library.magicindicator.buildins.commonnavigator.titles.badge.BadgeRule;


import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: demo_sc
 * @Package: com.njpl.fishplace.utils
 * @ClassName: PublicMethodUtils
 * @Description: 工具类
 * @Author: 作者名
 * @CreateDate: 2020/4/28 15:20
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/4/28 15:20
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class PublicMethodUtils {

    public static void initMagicIndicator(MagicIndicator magicIndicator, ViewPager viewPager, String[] titles, Context context) {
        magicIndicator.setBackgroundColor(Color.parseColor("#ffffff"));
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setScrollPivotX(0.25f);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context, 12);
                simplePagerTitleView.setText(titles[index]);
                simplePagerTitleView.setNormalColor(Color.parseColor("#1D2129"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#165DFF"));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setYOffset(UIUtil.dip2px(context, 2));
                indicator.setColors(Color.parseColor("#165DFF"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    public static void initMagicIndicatorBadgePager(MagicIndicator magicIndicator, ViewPager viewPager, String[] titles, int[] badge, Context context) {
        magicIndicator.setBackgroundColor(Color.parseColor("#ffffff"));
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setScrollPivotX(0.25f);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                final BadgePagerTitleView badgePagerTitleView = new BadgePagerTitleView(context);

                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles[index]);
                simplePagerTitleView.setNormalColor(Color.parseColor("#4E5969"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#1D2129"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
//                        badgePagerTitleView.setBadgeView(null); // cancel badge when click tab
                    }
                });
                badgePagerTitleView.setInnerPagerTitleView(simplePagerTitleView);

                if (null != badge) { // setup badge
                    TextView badgeTextView = (TextView) LayoutInflater.from(context).inflate(R.layout.simple_count_badge_layout, null);
                    badgeTextView.setText("" + badge[index]);
                    if (badge[index] == 0) {
                        badgePagerTitleView.setBadgeView(null); // cancel badge when click tab
                    } else {
                        badgePagerTitleView.setBadgeView(badgeTextView);
                        badgePagerTitleView.setXBadgeRule(new BadgeRule(BadgeAnchor.CONTENT_RIGHT, -UIUtil.dip2px(context, 6)));
                        badgePagerTitleView.setYBadgeRule(new BadgeRule(BadgeAnchor.CONTENT_TOP, 0));

                        // don't cancel badge when tab selected
                        badgePagerTitleView.setAutoCancelBadge(false);
                    }
                } else
                    badgePagerTitleView.setBadgeView(null); // cancel badge when click tab


                return badgePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setYOffset(UIUtil.dip2px(context, 2));
                indicator.setColors(Color.parseColor("#3DA1FC"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    public static void initMagicIndicator(MagicIndicator magicIndicator, ViewPager viewPager, String[] titles, Context context, boolean mAdjustMode) {
        magicIndicator.setBackgroundColor(Color.parseColor("#ffffff"));
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setAdjustMode(mAdjustMode);
        commonNavigator.setScrollPivotX(0.25f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(titles[index]);
                simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#3DA1FC"));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setYOffset(UIUtil.dip2px(context, 2));
                indicator.setColors(Color.parseColor("#3DA1FC"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    public static void initMagicIndicator(MagicIndicator magicIndicator, ViewPager viewPager, String[] titles, Context context, boolean mAdjustMode, boolean isLogin) {
        magicIndicator.setBackgroundColor(Color.parseColor("#ffffff"));
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setAdjustMode(mAdjustMode);
        commonNavigator.setScrollPivotX(0.25f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(titles[index]);
                simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#3DA1FC"));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setOnClickListener(v -> {

                    viewPager.setCurrentItem(index);
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setYOffset(UIUtil.dip2px(context, 2));
                indicator.setColors(Color.parseColor("#3DA1FC"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    public static void initMagicIndicator(MagicIndicator magicIndicator, ViewPager viewPager, List<String> titles, Context context) {
        magicIndicator.setBackgroundColor(Color.parseColor("#ffffff"));
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setScrollPivotX(0.25f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(titles.get(index));
                simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#3DA1FC"));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setYOffset(UIUtil.dip2px(context, 2));
                indicator.setColors(Color.parseColor("#3DA1FC"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }


}
